(function(window) {
    var appCtrl = {
        bindEven: function() {
            var bindings = [{
                    element: '.questchoice_1 dd', //单选
                    event: hiApp.device.click,
                    handler: this.questchoice_1
                }, {
                    element: '.questchoice_2_3 dd', //多选
                    event: hiApp.device.click,
                    handler: this.questchoice_2_3
                },
   //              {
   //              element: '.popover_button',
   //              event: hiApp.device.click,
   //              handler: this.popover
   //          },
			// {
   //              element: '.popover_hover',
   //              event: "mouseover",
   //              handler: this.popover
   //          },
                {
                    element: 'body',
                    event: hiApp.device.click,
                    handler: this.body_click
                }
            ];
            bindEvents(bindings);
        },
        questchoice_2_3: function() {
            var data = $(this).find('.icon-radio').attr('data');
            if (data == 1) {
                $(this).find('.icon-radio').attr('data', 0);
                $(this).find('.icon-radio').removeClass('checked');
            } else {
                $(this).find('.icon-radio').attr('data', 1);
                $(this).find('.icon-radio').addClass('checked');
            }
        },
        questchoice_1: function() {
            $(this).parent().find('.icon-radio').removeClass('checked');
            $(this).find('.icon-radio').addClass('checked');
        },
        body_click: function() {
            //alert(123);
            //$(".popover_button").popover("hide");
            //$("body").find(".popover").remove();

        },

        init: function(page) {
            $(".pages").height($(window).height());
            appCtrl.bindEven();
            this.showToolbar();
           // appCtrl.setToolbar(page);
           // appCtrl.hideToolbar(page);
            

        },
        afterShow:function(page){
          //console.log(page);
          //alert(1);
          appCtrl.setToolbar(page);
          appCtrl.hideToolbar(page);
        },
        //当前选中菜单
        setToolbar: function(page) {
            if (typeof(page) != "object") page = {};
            var s = "";
            var a = "";
            var i = 0;
            var name = "";
            var page_name = page.name;
            $.each(page.query, function(k, v) {
                if (i == 0) {
                    s = k;
                    a = v;
                }
                i++;
            })
            switch (s) {
                case 'Index':
                    name = "Index";
                    break;
                case 'User':
                    name = "User";
                    break;
                case 'Subject':
                    name = "Home";
                    break;
            }

            switch (page_name) {
                case 'Index_index':
                    name = "Index";
                    break;

                case 'Index_my_subject':
                    name = "Index";
                    break;
                case 'Subject_index':
                    name = "Home";
                    break; 
                case 'User_index':
                    name = "User";
                    break;        
            }
            //alert(page_name);
           
            $(".toolbar .toolbar-inner a").removeClass("active");
            $(".toolbar .toolbar-inner").find("[name='" + name + "']").addClass("active");
            

        },

        hideToolbar:function(page){
           if (hiApp.device.pc) {
            $(".toolbar").hide();
            return;
           }

           var page_name = page.name;
           if(page_name=="Index_index" || page_name=="Subject_index" || page_name=="Index_my_subject" || page_name=="User_index" ){
             $(".toolbar").show();
           }else{
             $(".toolbar").hide();
              
           }

          
        },
        //上传图片
        upload_input: function(e) {
            e = e || window.event;
            var fileList = e.target.files;
            if (!fileList.length) {
                return false;
            }
            var file = fileList[0];
            if (!photo.checkPicSize(file)) {
                hiApp.alert('图片体积过大');
            }
            if (!photo.checkPicType(file)) {
                hiApp.alert('上传照片格式不支持');
            }

            var id = Date.now();
            // 增加到上传对象中, 上传完成后，修改为true
            photo.uploadInfo[id] = {
                file: file,
                filename: '',
                isDone: false,
            };
            $('.up_pic_thumb').find('li').attr('id', 'li' + id);
            photo.previewQueue.push(id);
            $(this).val('');
        },
        showToolbar: function() {
            //console.log(hiApp.device.pc);
            if (hiApp.device.pc) {
                $(".toolbar").hide();
                return;
            }
            $(".toolbar").show();
            // console.log(213);
            if ($(".toolbar .toolbar-inner").length > 0) return;
            var html = $("#toolbarTemplate").html();
            //alert(html);
            $(".toolbar").html(html);
            hiApp.bind($(".toolbar"));


        },

        /**********图片浏览开始呀***/
        photoBrowser: function() {
            var url = [];
            var index = $(this).attr('index');
            var cur_img = $(this).attr('src');
            //alert(cur_img);
            var images = $(this).parents(".photoBrowser").find('img');
            for (i = 0; i < images.length; i++) {
                var img = {};
                var src = images.eq(i).attr('src');
                var img_data = images.eq(i).attr('data');
                if (typeof(img_data) != "string") {
                    img_data = src;
                }
                var img_title = images.eq(i).attr('alt');
                if (typeof(img_title) == "string") {
                    img_title = "";
                }
                img['title'] = img_title;
                img['href'] = img_data;
                url[i] = img;
                if (cur_img == src) {
                    index = i;
                }
            }
            if (!(index > 0)) {
                index = 0;
            }

            var options = {
                initialIndexOnArray: index,
            };
            $.swipebox(url, options);
        },
        /*********图片浏览到这里结束***/
    };

    window.BoxEdu_appCtrl = appCtrl;

})(window);
